
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider_demo/net/api.dart';
import 'package:provider_demo/provide/view_state.dart';

class ViewStateModel with ChangeNotifier {
  /// 防止页面销毁后,异步任务才完成,导致报错
    bool _disposed = false;
    /// 当前的页面状态,默认为busy,可在viewModel的构造方法中指定;
    ViewState _viewState;

    //子类可以在构造函数指定需要的页面状态
    ViewStateModel({ViewState viewState  = ViewState.idle})
        :_viewState = viewState {
       debugPrint('ViewStateModel---constructor--->$runtimeType');
    }

    ViewState get viewState => _viewState;
    set viewState(ViewState viewState) {
      _viewStateError = null;
      _viewState = viewState;
      notifyListeners();
    }
    ViewStateError _viewStateError;
    ViewStateError get viewStateError => _viewStateError;

    bool get isBusy => viewState == ViewState.busy;

    bool get isIdle => viewState == ViewState.idle;

    bool get isEmpty => viewState == ViewState.empty;

    bool get isError => viewState == ViewState.error;

    void setIdle() {
      viewState = ViewState.idle;
    }

    void setBusy() {
      viewState = ViewState.busy;
    }

    void setEmpty() {
      viewState = ViewState.empty;
    }
    void onError(ViewStateError viewStateError) {}
    /// [e]分类Error和Exception两种
    void setError(e, stackTrace, {String message}) {
      ViewStateErrorType errorType = ViewStateErrorType.defaultError;

      /// 见https://github.com/flutterchina/dio/blob/master/README-ZH.md#dioerrortype
      if (e is DioError) {
        if (e.type == DioErrorType.CONNECT_TIMEOUT ||
            e.type == DioErrorType.SEND_TIMEOUT ||
            e.type == DioErrorType.RECEIVE_TIMEOUT) {
          // timeout
          errorType = ViewStateErrorType.networkTimeOutError;
          message = '连接超时';
        } else if (e.type == DioErrorType.RESPONSE) {
          // incorrect status, such as 404, 503...
          message = '网络错误';
        } else if (e.type == DioErrorType.CANCEL) {
          // to be continue...
          message = '请求已取消';
        } else {
          // dio将原error重新套了一层
          e = e.error;
          if (e is UnAuthorizedException) {
            stackTrace = null;
            errorType = ViewStateErrorType.unauthorizedError;
          } else if (e is NotSuccessException) {
            stackTrace = null;
            message = e.message;
          }else if (e is NotSuccessException) {
            stackTrace = null;
            message = e.message;
          }else if (e is SocketException) {
            errorType = ViewStateErrorType.networkTimeOutError;
            message = e.message;
          } else {
            message = '网络错误,请稍后重试';
          }
        }
      }
      viewState = ViewState.error;
      _viewStateError = ViewStateError(
        errorType,
        message: message,
        errorMessage: e.toString(),
      );
      printErrorStack(e, stackTrace);
      onError(viewStateError);
    }



}

printErrorStack(e, s) {
  debugPrint('''
<-----↓↓↓↓↓↓↓↓↓↓-----error-----↓↓↓↓↓↓↓↓↓↓----->
$e
<-----↑↑↑↑↑↑↑↑↑↑-----error-----↑↑↑↑↑↑↑↑↑↑----->''');
  if (s != null) debugPrint('''
<-----↓↓↓↓↓↓↓↓↓↓-----trace-----↓↓↓↓↓↓↓↓↓↓----->
$s
<-----↑↑↑↑↑↑↑↑↑↑-----trace-----↑↑↑↑↑↑↑↑↑↑----->
    ''');
}
